package cn.tkb.pet.controller;

import cn.tkb.basic.exception.BusinessException;
import cn.tkb.basic.util.AjaxResult;
import cn.tkb.basic.util.LoginContext;
import cn.tkb.basic.util.PageQuery;
import cn.tkb.org.domain.Department;
import cn.tkb.org.query.DepartmentQuery;
import cn.tkb.pet.domain.Pet;
import cn.tkb.pet.query.PetQuery;
import cn.tkb.pet.service.IPetService;
import cn.tkb.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;
    /**
     * 该接口支持前端和后端查询，请传入必要的参数
     * @param query
     * @return
     */
    @PostMapping
    public PageQuery<Pet> queryPage(@RequestBody PetQuery query){
        return petService.findPage(query);
    }

    /**
     * 宠物上架
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onSale(@RequestBody List<Long>ids){

        try {
            petService.onSale(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("操作失败！！！");
        }
    }
    /**
     * 宠物下架
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offSale(@RequestBody List<Long>ids){

        try {
            petService.offSale(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("操作失败！！！");
        }
    }

    /**
     * 根据宠物的id查询宠物详情
     * @param petId
     * @return
     */
    @GetMapping("/detail/{petId}")
    public Pet queryByDetailId(@PathVariable("petId") Long petId){
        return petService.queryByDetailId(petId);
    }

    /**
     * 领养宠物
     * @param param
     * @param request
     * @return
     */
    @PostMapping("/adopt")
    public AjaxResult adopt(@RequestBody Map<String,Long> param, HttpServletRequest request){

        try {
            petService.adopt(param, LoginContext.getCurrentLoginInfo(request));
            return AjaxResult.success();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("领养失败！！！");
        }
    }

    /**
     * 通过宠物id查询宠物的基本信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Pet queryById(@PathVariable("id") Long id){
        return petService.QeuryById(id);
    }
}
